package com.yang.game;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.squareup.okhttp.Request;
import com.yang.game.utils.OkHttpClientManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ListActivity extends AppCompatActivity {

    private ListView listView;
    private List<Map<String, Object>> list = new ArrayList<>();

    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        listView = findViewById(R.id.userList);
        context = this;
        // 获取用户列表数据
        OkHttpClientManager.getAsyn("http://10.99.212.127:9090/api/user/all", new OkHttpClientManager.ResultCallback<List<Map<String, Object>>>() {
            @Override
            public void onError(Request request, Exception e) {
                Log.d("error", e.toString());
            }
            @Override
            public void onResponse(List<Map<String, Object>> response) {
                if (response == null) {
                    Toast.makeText(ListActivity.this, "用户信息获取失败", Toast.LENGTH_LONG).show();
                    return ;
                }
                list = response;
                SimpleAdapter adapter = new SimpleAdapter(context, list, R.layout.list_item, new String[]{"username", "score"},
                        new int[]{R.id.username, R.id.score});
                listView.setAdapter(adapter);
                Toast.makeText(ListActivity.this, "用户信息获取成功", Toast.LENGTH_LONG).show();
            }
        });
    }
}